#!/bin/sh

# udhcpc script edited by Tim Riker <Tim@Rikers.org>

[ -z "$1" ] && echo "Error: should be called from udhcpc" && exit 1
netPriroty=`cat /factory/net/netproority`
RESOLV_CONF="/etc/resolv.conf"
[ -n "$broadcast" ] && BROADCAST="broadcast $broadcast"
[ -n "$subnet" ] && NETMASK="netmask $subnet"
case "$1" in
	deconfig)
		/sbin/ifconfig $interface 0.0.0.0
		;;

	renew|bound)
		/sbin/ifconfig $interface $ip $BROADCAST $NETMASK
		
		if [ -n "$router" ] ; then
			
			echo $interface
			while /sbin/route del default gw 0.0.0.0 dev $interface; do
				:
			done

			for i in $router ; do
				echo "file data:$netPriroty" >>/tmp/card
				echo "netcard data: $interface" >>/tmp/card
				if [ $netPriroty = $interface ] ; then
					route add default gw $i dev $interface metric 0
				elif [ $interface = "usb0" ] ; then
					route add default gw $i dev $interface metric 10	
				elif [ $interface = "usb1" ] ; then
					route add default gw $i dev $interface metric 20
				elif [ $interface = "rmnet_usb0.1" ] ; then
					route add default gw $i dev $interface metric 30
				elif [ $interface = "rmnet_usb1.1" ] ; then
					route add default gw $i dev $interface metric 40
				elif [ $interface = "eth0" ] ; then
					route add default gw $i dev $interface metric 50
				else
					route add default gw $i dev $interface metric 60
				fi
			done
		fi

		echo -n > $RESOLV_CONF
		[ -n "$domain" ] && echo search $domain >> $RESOLV_CONF
		for i in $dns ; do
			echo adding dns $i
			echo nameserver $i >> $RESOLV_CONF
		done
		echo adding dns 114.114.114.114
		echo nameserver 114.114.114.114 >> $RESOLV_CONF
		echo adding dns 8.8.8.8
		echo nameserver 8.8.8.8 >> $RESOLV_CONF
		;;
esac
killall ntpd
/usr/sbin/ntpd &
exit 0
